unit DataModule;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,   FileUtil, LResources, Forms, Controls,
  Dialogs;

type

  { TDataModule1 }

  TDataModule1 = class(TDataModule)
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    ClassList   : TStringList;
    Classes     : TStringList;
    FirstNames  : TStringList;
    Teacher     : String;
    Year        : String;
    Group       : String;
    Filtered    : Boolean;
    Function ExtractName(Details : string): String;
    procedure LoadClassList;
    procedure InitData;
  end; 

var
  DataModule1: TDataModule1; 

implementation

{ TDataModule1 }

Procedure TDataModule1.InitData;
var
    FileHandle        : TextFile;
    InputString       : String;
    ClassCode : String;
    Delim2,Delim3,
    Delims,i,str      : Integer;
begin
     if Classes.Count > 0 then
        Classes.Clear;
     Classes.Duplicates:= dupIgnore;
     Classes.BeginUpdate;
     AssignFile(FileHandle, 'Classes.csv');
     Reset(FileHandle);
     while not Eof(FileHandle) do
     begin
          Readln(FileHandle,InputString);
          if Teacher <> '' then
          begin
               if (Pos(Teacher,InputString) = 0) then continue;
          end;
          str := length(InputString);
          i := 0;
          Delims := 0;
          while (i < str)  do
          begin
               i := i + 1;
               if (IsDelimiter(',',inputstring,i)) then
               begin
                    Delims := Delims + 1;
                    if Delims = 2 then Delim2 := i;
                    if Delims = 3 then Delim3 := i;
               end;
               if  Delims = 3 then break;
          end;
          ClassCode := Copy(InputString,Delim2+1,Delim3-Delim2-1);
          if Year <> '' then
          begin
               if (LeftStr(ClassCode,Length(Year)) <> Year) then Continue;
          end;
          Classes.Add(ClassCode);
     end;
     Classes.EndUpdate;
     CloseFile(FileHandle);
end;

procedure TDataModule1.DataModuleCreate(Sender: TObject);
begin
  ClassList         := TStringList.Create;
  FirstNames        := TStringList.Create;
  Classes           := TStringList.Create;
  Filtered          := False;
  Classes.Sorted    := True;
  Classes.Duplicates:= dupIgnore;
//  InitData;
end;

procedure TDataModule1.DataModuleDestroy(Sender: TObject);
begin
  ClassList.Free;
  FirstNames.Free;
  Classes.Free;
  inherited Destroy;
end;

function TDataModule1.ExtractName(Details: string): String;
var
  i,str  : Integer;
  Remainder     : string;
  Delims       : Integer;

begin
     str := Length(Details);
     i := 0;
     Delims := 0;
     while (i < str)  do
     begin
         i := i + 1;
         if (IsDelimiter(',',details,i)) then Delims := Delims + 1;
         if  Delims = 2 then break;
     end;
     Remainder := LeftStr(Details,i-1);
     ExtractName := Remainder;
end;

procedure TDataModule1.LoadClassList;
var
    FileHandle        : TextFile;
    InputString       : String;
    Fullname,Forename : String;
    ForenamePos       : Integer;
begin
     if ClassList.Count > 0 then
        ClassList.Clear;
     if FirstNames.Count > 0 then
        FirstNames.Clear;
     AssignFile(FileHandle, 'Classes.csv');
     Reset(FileHandle);
     while not Eof(FileHandle) do
     begin
          Readln(FileHandle,InputString);
          if pos(Group,InputString) > 0 then
          begin
             FullName := ExtractName(InputString);
             ClassList.Add(FullName);
             ForenamePos := LastDelimiter(',',FullName);
             Forename    := Copy(Fullname,ForenamePos + 1,Length(FullName));
             FirstNames.Add(Forename);
          end;
     end;
     CloseFile(FileHandle);
end;

initialization
  {$I datamodule.lrs}

end.

